import { init as pluginInit, updateSGUserInfo, getConnectionState } from './plugin';
export * from './request';
import { login } from './request';
import { modules } from './config';
export * from './api';
export * from './deviceIcon';
export * from './wx'

export { updateSGUserInfo, getConnectionState };

export const init = (userId) => {

  // 为了不污染正式环境的数据，所以测试环境都统一采用 userId 为1的数据来作为绑定，

  if (global.IS_DEV) {
    userId = 1;
  }

  getApp().globalData.associatedId = userId;
  
  // 获取token
  login({ associatedId: userId })

  pluginInit(userId);
}

export const uninit = () => {

}


/**
 * 跳转拾果h5 默认navigateTo方式
 * @param {String} originUrl 要跳转的h5页面url
 * @param {Boolean} replace 是否使用redirectTo
 */
export function openWebView(originUrl, replace = false) {
  let baseUrl = 'https://h5.leshiguang.com';
  if (originUrl) {
    originUrl = originUrl.indexOf('http') > -1 ? originUrl : baseUrl + originUrl;
    // 插入版本号
    let appName = /\.com\/(\w+)\//.test(originUrl) ? RegExp["$1"] : '';
    let version = '0.0.1';
    if (appName && modules) {
      version = modules[appName] || '0.0.1'
    }
    let tn = getApp().globalData.tn;
    originUrl = originUrl.replace(`/${appName}/`, `/t/${appName}/${version}/${tn}/`);
    let url = `/pages/h5/common/main?originUrl=${encodeURIComponent(originUrl)}`
    console.warn('utl', url);
    if (!replace) {
      wx.navigateTo({ url })
    } else {
      wx.redirectTo({ url });
    }
  }
}

/**
 * 跳转拾果h5 redirectTo方式
 * @param {String} originUrl 要跳转的h5页面url
 */
export function redirectOpenWebView (originUrl) {
  openWebView(originUrl, true);
}

